

package com.yz.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yz.common.entity.*;
import com.yz.common.utils.R;
import com.yz.dao.DrillingDao;
import com.yz.dao.RuinDao;
import com.yz.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service("drillingService")
public class DrillingServiceImpl extends ServiceImpl<DrillingDao,DrillingEntity> implements DrillingService {
    @Autowired
    CheckService checkService;
    @Autowired
    DrillingDetailService drillingDetailService;
    @Autowired
    ProjectService projectService;

    @Override
    public long selectId() {
        return this.baseMapper.selectId();
    }
    @Override
    @Transactional(rollbackFor=Exception.class)
    public void postDrilling(DrillingForm t) {
        DrillingEntity data = t.getDrilling();
        CheckEntity check = new CheckEntity();
        check.setCreateAt(data.getUpdateAt());
        check.setCreateUser(data.getCreateUser());
        check.setName(projectService.selectById(data.getProjectId()).getName());
        check.setNameForm(contant.type_name_form[3]);
        check.setProjectId(data.getProjectId());
        check.setStatus(1);
        check.setType(5);
        check.setTypeForm(4);
        long id = 0;
        if(data.getId()==0){
            this.baseMapper.insert(data);
            id = this.baseMapper.selectId();
            check.setDataId(id);
            check.setNumber(contant.type_all_form[4]+contant.type_detail_form[3]+id);
            checkService.insert(check);
        }else{
            id = data.getId();
            check.setDataId(id);
            check.setNumber(contant.type_all_form[4]+contant.type_detail_form[3]+id);
            checkService.update(check,new EntityWrapper<CheckEntity>().eq("number",contant.type_all_form[4]+contant.type_detail_form[3]+id));
            this.baseMapper.update(data,new EntityWrapper<DrillingEntity>().eq("id",id));
            drillingDetailService.delete(new EntityWrapper<DrillingDetailEntity>().eq("drilling_id",id));
        }
        List<DrillingDetailEntity> details = t.getDetails();
        for (int i = 0; i < details.size(); i++) {
            DrillingDetailEntity item = details.get(i);
            item.setDrillingId(id);
            drillingDetailService.insert(item);
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public DrillingForm postDrillingDetail(long id) {
        DrillingEntity t = new DrillingEntity();
        t.setId(id);
        DrillingEntity drilling = this.baseMapper.selectById(t);
        List<DrillingDetailEntity> details = this.baseMapper.selectDetails(id);
       DrillingForm data = new DrillingForm();
       data.setDetails(details);
       data.setDrilling(drilling);
        return data;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void deleteDrilling(long id) {
        checkService.deleteMe(5,4,id);
        this.baseMapper.deleteById(id);
        this.baseMapper.deleteDetails(id);
        drillingDetailService.delete(new EntityWrapper<DrillingDetailEntity>().eq("drilling_id",id));
    }
}